package com.koron.util.secret;

import com.koron.css2.serviceManage.utils.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.Map;

@Component
public class Tools {

	private static String apAppId;// 应用id

	@Value("${ap.identify.appId}")
	public void setApAppId(String apAppId) {
		this.apAppId = apAppId;
	}

	private static Logger logger = LogManager.getLogger(Tools.class);

	public static MessageBean callMgInterface(String url, Object data) {
		try {
			RequestBean reqBean = new RequestBean<>();
			reqBean.setUrl(url);
			reqBean.setData(data);
			return JsonUtils.jsonToPojo(SecretReqUtil.sendToGatWay(reqBean), MessageBean.class);
		} catch (Exception e) {
			logger.error("调用中台接口失败" + url, e);
			throw new RuntimeException("调用中台接口失败");
		}
	}

	public static MessageBean callDataPushInterface(Integer type, Integer opType, Object data) {
		try {
			RequestBean reqBean = new RequestBean<>();
			reqBean.setUrl(StaticInfo.dataPushUrl);
			Map<String, Object> map = new HashMap<>();
			map.put("type", type);
			map.put("opType", opType);
			map.put("data", JsonUtils.objectToJson(data));
			reqBean.setData(map);
			reqBean.setAppid(apAppId);
			return JsonUtils.jsonToPojo(SecretReqUtil.sendToGatWay(reqBean), MessageBean.class);
		} catch (Exception e) {
			logger.error("调用应用平台数据推送接口失败", e);
			throw new RuntimeException("调用数应用平台据推送接口失败");
		}
	}
}